// miniprogram/pages/gank/gank.js
const app = getApp()
const util = require('../../utils/util.js')
const AV = require('../../libs/leancloud/av-weapp-min.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 显示控制
        show: true,
        // 广告位控制
        adShow: false,
        ad6Show: false,
        ad6ID: "",
        ad7Show: false,
        ad7ID: "",
        
        defaultBG: '../../images/test.jpg',
        passwordBG: '../../images/password.png',

        currentPageIndex:[
            0,0,0,0
        ],

        // 加载相关
        isFirsttimeLoad: [
            false, true, true, true, true
        ],
        isLastPage: [
            false, false, false, false
        ],
        isLoading: [
            false, false, false, false
        ],
        blogDataList: [

        ],
        blogDataPageNum: 0,
        // 所有标签
        blogTagDataList: [
        ],
        // 某个标签数据
        blogSpecifyTagDataList: [

        ],
        tagsPageNum: {
            "全部": 0            
        },
        currentTag: '全部',
        currentTagPageIndex: {

        },
        // Menubar设置       
        tabbarHeight: 0,
        menuBarHeight: 0,
        hotItems: [
            { id: 0, text: '浏览最多' },
            { id: 1, text: '评论最多' },
            { id: 2, text: '点赞最多' },
            { id: 3, text: '收藏最多' }
        ],
        hotCurIndex: 0,
        hotLastIndex: 0,
        labelCurIndex: -1,
        gankRankingList: [

        ],
        // Tabbar设置
        currentIndex: 0,
        list: [{
            id: 0,
            color: "#464c5b",
            selectedColor: "#3498DB",
            text: "最新",
            borderBottom: 'border-bottom: solid 2rpx #3498DB',
            style: 'font-size: 34rpx;padding-top: 15rpx;padding-bottom: 15rpx;',
            badge: ''
        },
        {
            id: 1,
            color: "#464c5b",
            selectedColor: "#3498DB",
            text: "热门",
            borderBottom: 'border-bottom: solid 2rpx #3498DB',
            style: 'font-size: 34rpx;padding-top: 15rpx;padding-bottom: 15rpx;',
            badge: ''
        },
        {
            id: 2,
            color: "#464c5b",
            selectedColor: "#3498DB",
            text: "标签",
            borderBottom: 'border-bottom: solid 2rpx #3498DB',
            style: 'font-size: 34rpx;padding-top: 15rpx;padding-bottom: 15rpx;',
            badge: ''
        }
        ]
    },
    checkoutAD: function () {
        let that = this;
        if (app.globalData.config) {
            that.data.defaultBG = app.globalData.config.global.default_bg;
            that.data.adShow = app.globalData.config.ad.show;
            that.data.ad6Show = app.globalData.config.ad.ad6.show;
            that.data.ad6ID = app.globalData.config.ad.ad6.id;
            that.data.ad7Show = app.globalData.config.ad.ad7.show;
            that.data.ad7ID = app.globalData.config.ad.ad7.id;
            that.setData({
                defaultBG: that.data.defaultBG,
                adShow: that.data.adShow,
                ad6Show: that.data.ad6Show,
                ad6ID: that.data.ad6ID,
                ad7Show: that.data.ad7Show,
                ad7ID: that.data.ad7ID
            })
        }
    },    
    adLoadError: function (e) {
        console.log(e.detail)
        let that = this;
        let index = e.currentTarget.dataset.index;
        that.data.blogDataList[index].cover_show = true
        that.setData({
            blogDataList: that.data.blogDataList
        })
    },
    adLoadSuccess: function (e) {
        console.log('Banner广告加载成功')
        let that = this;
        let index = e.currentTarget.dataset.index;
        that.data.blogDataList[index].cover_show = false
        that.setData({
            blogDataList: that.data.blogDataList
        })
    },
    calcTabbarHeight: function () {
        let that = this;
        const query = wx.createSelectorQuery()
        query.select('#mp_tabbar').boundingClientRect()
        query.selectViewport().scrollOffset()
        query.exec(function (res) {
            //console.log(res)
            that.data.tabbarHeight = res[0].height;
            that.setData({
                tabbarHeight: that.data.tabbarHeight
            })
        })
    },
    calcMenuBarHeight: function (index) {
        let that = this;
        const query = wx.createSelectorQuery()
        // 获取高度，需要设置view的高度，height: auto;无效
        query.select('#menu_' + index).boundingClientRect()
        query.exec(function (res) {
            that.data.menuBarHeight = res[0].height;
            that.setData({
                menuBarHeight: that.data.menuBarHeight
            })
            //console.log(that.data.menuBarHeight)
        })
    },
    tabChange(e) {
        let that = this;
        that.setData({
            currentIndex: e.detail.index
        }, function () {
            that.calcMenuBarHeight(e.detail.index)
        })

        if (!that.data.show) {
            return;
        }

        // 热门
        if (e.detail.index == 1) {
            if (that.data.hotCurIndex == 0) {
                // 浏览最多
                that.getTopNGankData('time', 15, function(status) {
                    if (status) {
                        that.data.hotLastIndex = that.data.hotCurIndex
                    }
                })
            }
        }
        // 获取数据
        // 标签
        if (e.detail.index == 2) {            
            // 获取网络数据
            if (that.data.isFirsttimeLoad[2] && !that.data.isLoading[2]) {
                let loading2 = 'isLoading[2]';
                that.setData({
                    [loading2]: true
                })
                that.fetchTagsInfo(function (status, result) {
                    let loading2 = 'isLoading[2]';
                    that.setData({
                        [loading2]: false
                    })
                    if (status) {
                        that.setData({
                            blogTagDataList: result,
                            blogSpecifyTagDataList: that.data.blogDataList
                        })
                        that.data.isFirsttimeLoad[2] = false;
                    } else {
                        // 加载标签信息数据失败   
                    }
                    // 一次加载，不分页
                    that.data.isLastPage[2] = true;
                    that.setData({
                        isLastPage: that.data.isLastPage
                    })
                });
            }
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        that.calcTabbarHeight();
        // 配置检查
        if (app.globalData.config) {        
            if (!app.globalData.config.tabbar[2].show) {
                that.data.isLastPage[0] = true;
                that.data.isLastPage[1] = true;
                that.data.isLastPage[2] = true;
                that.setData({
                    isLastPage: that.data.isLastPage,
                    show: false
                })                
                return ;
            }
        }
       
        that.checkoutAD();
        that.firstTimeLoadBlogPageData(function (status) {
            // 
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        console.log('下拉刷新')
        let that = this;
        if (!that.data.show) {
            wx.stopPullDownRefresh();
            return ;
        }


        if (that.data.currentIndex == 0) {
            // 重新请求首页博文
            that.firstTimeLoadBlogPageData(function (status) {
                // 主动停止下拉操作
                wx.stopPullDownRefresh();
            });   
        } else if (that.data.currentIndex == 1) {
            // TODO
            wx.stopPullDownRefresh();
        } else if (that.data.currentIndex == 2) {
            // TODO
            wx.stopPullDownRefresh();
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        console.log('onReachBottom')
        let that = this;
        if (!that.data.show) {         
            return;
        }

        let id = that.data.currentIndex;
        if ((!that.data.isLastPage[0] && id == '0' && !that.data.isLoading[0]) || (id == '2' && that.data.labelCurIndex == -1 && !that.data.isLastPage[0])) {
            let loading0 = 'isLoading[0]';
            that.setData({
                [loading0]: true
            })
            wx.showLoading({
                title: '加载中...',
            })
            that.tryToFetchBlogDataPageNum(function (status, result) {
                if (status) {
                    that.fetchBlogDataList(that.data.currentPageIndex[0], function (status) {
                        that.setData({
                            [loading0]: false,
                            blogSpecifyTagDataList: that.data.blogDataList
                        })
                        wx.hideLoading();
                    });                    
                } else {
                    // 获取博文总页数失败
                    that.setData({
                        [loading0]: false
                    })
                    wx.hideLoading();                
                }
            })
        } else if (id == '2') {
            // 标签            
            let loading2 = 'isLoading[2]';

            if (!that.data.isLastPage[2]) {
                that.setData({
                    [loading2]: true
                })
                wx.showLoading({
                    title: '加载中...'
                })
            } else {
                return ;
            }
            // TODO
            // 获取列表数据       
            let tagName = that.data.currentTag;
            that.tryToFetchSpecifyCategoryOrTagDataPageNum(tagName, function (status, result) {
                if (status) {
                    that.fetchSpecifyCategoryOrTagDataList(that.data.currentTagPageIndex[tagName], tagName, function (status, result) {
                        if (status) {
                            that.data.blogSpecifyTagDataList = [].concat(that.data.blogSpecifyTagDataList, result);
                            that.setData({
                                blogSpecifyTagDataList: that.data.blogSpecifyTagDataList,
                                isLastPage: that.data.isLastPage
                            })
                        } else {

                        }       
                        that.setData({
                            [loading2]: false
                        })        
                        wx.hideLoading()         
                    })
                } else {
                    that.setData({
                        [loading2]: false
                    })
                    wx.hideLoading()
                }
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    hotSelect: function (e) {       
        let that = this;
        
        that.setData({           
            hotCurIndex: e.target.dataset.id
        })
        if (!that.data.show) {
            return;
        }

        if (that.data.hotCurIndex != that.data.hotLastIndex) {
            let query_type = 'time'
            if (e.target.dataset.id == 1) {
                // 评论最多
                query_type = 'comments'
            } else if (e.target.dataset.id == 2) {
                // 点赞最多
                query_type = 'likes'
            } else if (e.target.dataset.id == 3) {
                // 收藏最多
                query_type = 'likes'
            }

            that.getTopNGankData(query_type, 15, function(status) {
                if (status) {
                    that.data.hotLastIndex = that.data.hotCurIndex
                }
            })
        }
        
    },
    labelSelect: function(e) {
        let that = this;
        if (!that.data.show) {
            return;
        }
        
        that.setData({
            labelCurIndex: e.target.dataset.id
        })

        if (e.target.dataset.key == '全部' && !that.data.isLoading[2]) {
            that.data.currentTag = '全部';
            that.data.blogSpecifyTagDataList = that.data.blogDataList;
            that.setData({
                blogSpecifyTagDataList: that.data.blogSpecifyTagDataList
            })
        } else {
            that.data.currentTag = e.target.dataset.key;
            if (!that.data.isLoading[2]) {
                let loading2 = 'isLoading[2]';
                that.data.isLastPage[2] = false;
                that.setData({
                    [loading2]: true,
                    isLastPage: that.data.isLastPage
                })
                wx.showLoading({
                    title: '加载中...',
                })
                // 获取指定标签数据
                that.firstTimeLoadTagPageData(e.target.dataset.key, function(){
                    let loading2 = 'isLoading[2]';
                    that.setData({
                        [loading2]: false                        
                    })
                    wx.hideLoading();
                });
            }
        }
    },
    tapRankingItem: function (e) {
        console.log(e);
        let title = e.currentTarget.dataset.title;
        let url = e.currentTarget.dataset.url;
        let url_split = url.split('/');
        let key = url_split[2].split('.')[0];
        let password = e.currentTarget.dataset.password;
        let password_desc = e.currentTarget.dataset.password_desc;

        wx.navigateTo({
            url: '../detail/detail?title=' + title + '&key=' + key + '&password=' + password 
                + '&password_desc=' + password_desc + '&is_gank=true',
            success: function () {
                // nothing to do
            },
            fail: function () {
                console.error("打开链接失败");
            }
        })
    },
    getTopNGankData: function(type, size, call_back) {
        let that = this;
        let isLastPage1 = 'isLastPage[1]';
        that.data.isLastPage[1] = false;
        that.setData({
            isLastPage: that.data.isLastPage            
        })
        wx.showLoading({
            title: '加载中...',
        })
        that.data.gankRankingList = []
        that.fetchTopNGank(type, size, function (status, results) {
            if (status) {
                //console.log(results);
                for (let i = 0; i < results.length; i++) {
                    // id: "0",
                    // title: "测试标题1",
                    // page_views: 200,
                    // comment_count: 10,
                    // thumbs_up_count: 11,
                    // date: "2019-10-05 10:49:00"                        
                    let item = {
                        id: results[i].id,
                        title: results[i].attributes.title,
                        page_views: results[i].attributes.time,
                        comment_count: results[i].attributes.comments,
                        thumbs_up_count: results[i].attributes.likeUserList == undefined ? 0 : results[i].attributes.likeUserList.length,
                        url: results[i].attributes.url,
                        date: util.getLocalDateTimeStr(results[i].createdAt),
                        password: results[i].attributes.password ? results[i].attributes.password : '',
                        password_desc: results[i].attributes.password_desc ? results[i].attributes.password_desc: ''
                    }
                    that.data.gankRankingList.push(item)
                }

                that.data.isLastPage[1] = true;
                //console.log(that.data.gankRankingList)
                that.setData({
                    isLastPage: that.data.isLastPage,
                    gankRankingList: that.data.gankRankingList
                })
                wx.hideLoading()
                call_back(true)
            } else {
                that.data.isLastPage[1] = true;
                that.setData({
                    isLastPage: that.data.isLastPage
                })
                wx.hideLoading()
                call_back(false)
            }
        })
    },
    fetchTopNGank: function (property, limit, call_back) {
        var query = new AV.Query('Counter');
        query.descending(property);
        query.contains('url', '.html');
        query.contains('tag', 'gank');
        query.limit(limit);
        query.find().then(ret => {
            //console.log(ret)
            call_back(true, ret);
        }).catch(ex => {
            console.log(ex.message);
            console.log(ex.code);
            if (ex.code == 101) {
                console.log('页面统计表未存在');
            }
            wx.showToast({
                title: '加载博文排行榜失败',
                icon: 'none',
                duration: 2000
            })
            call_back(false, null);
        });
    },
    // 数据加载相关    
    firstTimeLoadBlogPageData: function (call_back) {
        let that = this;
        that.data.currentPageIndex[0] = 0;
        that.data.isLastPage[0] = false;
        that.data.blogDataList = [];

        if (!that.data.isLastPage[0]) {
            wx.showLoading({
                title: '加载中...',
            })
            setTimeout(function () {
                wx.hideLoading()
            }, 8000)
            that.tryToFetchBlogDataPageNum(function (status, result) {
                if (status) {                    
                    that.fetchBlogDataList(that.data.currentPageIndex[0], function (status) {
                        // status为true，则获取数据成功
                        wx.hideLoading();
                        call_back(true)
                    });
                } else {
                    // 获取博文总页数失败
                    wx.hideLoading();
                    wx.showToast({
                        title: '获取博文出了点问题',
                        icon: 'none',
                        duration: 2000
                    });
                    that.data.isLastPage[0] = true;
                    that.setData({
                        isLastPage: that.data.isLastPage
                    })
                    call_back(false)
                }
            })
        }
    },
    tryToFetchBlogDataPageNum: function (call_back) {
        let that = this;
        if (that.data.blogDataPageNum > 0) {
            // 已经获取
            call_back(true, that.data.blogDataPageNum);
            return;
        }

        wx.request({
            url: app.globalData.gitee.gank_output_url + '/blog_time_top_pages/page.json',
            data: {
                access_token: app.globalData.gitee.access_token,
                ref: app.globalData.gitee.branch
            },
            success: function (res) {
                console.log('请求博文页数数据成功');
                let output = app.base64.decode(res.data.content);
                let parse_res = JSON.parse(output);
                that.data.blogDataPageNum = parse_res.total_pages;
                call_back(true, that.data.blogDataPageNum);
            },
            fail: function (e) {
                console.log('请求博文页数数据失败');
                call_back(false, null);
            }
        })
    },
    fetchBlogDataList: function (page_num, call_back) {
        let that = this;
        wx.request({
            url: app.globalData.gitee.gank_output_url + '/blog_time_top_pages/' + page_num + '.json',
            data: {
                access_token: app.globalData.gitee.access_token,
                ref: app.globalData.gitee.branch
            },
            success: function (res) {
                console.log('请求博文页列表数据成功');
                let output = app.base64.decode(res.data.content);
                let parse_res = JSON.parse(output);
                // 当前页偏移+1
                that.data.currentPageIndex[0] = that.data.currentPageIndex[0] + 1;
                if (that.data.currentPageIndex[0] >= that.data.blogDataPageNum) {
                    that.data.isLastPage[0] = true;
                }

                //console.log(parse_res);
                let keys = [];
                for (let i = 0; i < parse_res.length; i++) {
                    let key = '/' + parse_res[i].key.substr(0, 4) + '/' + parse_res[i].key + '.html';
                    keys.push(key);
                }

                that.fetchArticleStatisticsData(keys, function (status, result) {
                    if (status) {
                        for (let i = 0; i < parse_res.length; i++) {
                            let j = 0;
                            for (j; j < result.length; j++) {
                                let key = '/' + parse_res[i].key.substr(0, 4) + '/' + parse_res[i].key + '.html';
                                if (key == result[j].attributes.url) {
                                    parse_res[i]["page_views"] = result[j].attributes.time == undefined ? 0 : result[j].attributes.time;
                                    parse_res[i]["comment_count"] = result[j].attributes.comments == undefined ? 0 : result[j].attributes.comments;
                                    parse_res[i]["thumbs_up_count"] = result[j].attributes.likeUserList == undefined ? 0 : result[j].attributes.likeUserList.length;
                                    break;
                                }
                            }
                            if (j == result.length) {
                                // 还未创建文章统计数据
                                parse_res[i]["page_views"] = 0;
                                parse_res[i]["comment_count"] = 0;
                                parse_res[i]["thumbs_up_count"] = 0;
                            }
                        }
                        that.data.blogDataList = [].concat(that.data.blogDataList, parse_res);
                        
                        that.setData({
                            blogDataList: that.data.blogDataList,
                            isLastPage: that.data.isLastPage
                        })
                        // 回调返回结果
                        call_back(true)
                    } else {
                        // 获取每页文章统计数据失败
                        call_back(false)
                    }
                });
            },
            fail: function (e) {
                console.log('请求博文页列表数据失败');
                call_back(false);
            }
        })
    },
    fetchArticleStatisticsData: function (keys, call_back) {
        let query = new AV.Query('Counter');
        query.containedIn('url', keys);
        query.find().then(ret => {
            //console.log(ret)
            call_back(true, ret);
        }).catch(ex => {
            console.log(ex.message);
            console.log(ex.code);
            if (ex.code == 101) {
                console.log('页面统计表未存在');
            }
            call_back(false, null);
        });
    },
    // 标签
    fetchTagsInfo: function (call_back) {
        wx.request({
            url: app.globalData.gitee.gank_output_url + '/blog_time_tags_pages/tags.json',
            data: {
                access_token: app.globalData.gitee.access_token,
                ref: app.globalData.gitee.branch
            },
            success: function (res) {
                console.log('请求博文标签信息数据成功');
                let output = app.base64.decode(res.data.content);
                let parse_res = JSON.parse(output);
                call_back(true, parse_res);
            },
            fail: function (e) {
                console.log('请求博文标签信息数据失败');
                call_back(false, null);
                wx.showToast({
                    title: '获取博文标签数据出了点问题',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },
    // 分页加载标签
    firstTimeLoadTagPageData: function(tagName, call_back) {
        let that = this;
        that.data.tagsPageNum[tagName] = 0;
        that.data.currentTagPageIndex[tagName] = 0;
        that.data.blogSpecifyTagDataList = [];
        // 获取列表数据       
        that.tryToFetchSpecifyCategoryOrTagDataPageNum(tagName, function (status, result) {
            if (status) {
                that.fetchSpecifyCategoryOrTagDataList(that.data.currentTagPageIndex[tagName], tagName, function (status, result) {
                    if (status) {
                        that.data.blogSpecifyTagDataList = [].concat(that.data.blogSpecifyTagDataList, result);
                        that.setData({
                            blogSpecifyTagDataList: that.data.blogSpecifyTagDataList,
                            isLastPage: that.data.isLastPage
                        })
                    } else {

                    }
                    call_back()             
                })
            } else {
                call_back()
            }
        })
    },
    tryToFetchSpecifyCategoryOrTagDataPageNum: function (dir_name, call_back) {
        let that = this;
        if (that.data.tagsPageNum[dir_name] && that.data.tagsPageNum[dir_name] > 0) {
            // 已经获取
            call_back(true, that.data.tagsPageNum[dir_name]);
            return;
        }

        wx.request({
            url: app.globalData.gitee.gank_output_url + '/blog_time_tags_pages/' + dir_name + '/page.json',
            data: {
                access_token: app.globalData.gitee.access_token,
                ref: app.globalData.gitee.branch
            },
            success: function (res) {
                console.log('请求标签页数数据成功');
                let output = app.base64.decode(res.data.content);
                let parse_res = JSON.parse(output);
                that.data.tagsPageNum[dir_name] = parse_res.total_pages;
                call_back(true, that.data.tagsPageNum[dir_name]);
            },
            fail: function (e) {
                console.log('请求标签页数数据失败');
                call_back(false, null);
            }
        })
    },
    fetchSpecifyCategoryOrTagDataList: function (page_num, dir_name, call_back) {
        let that = this;
        let target_dir = 'blog_time_tags_pages';        

        wx.request({
            url: app.globalData.gitee.gank_output_url + '/' + target_dir + '/' + dir_name + '/' + page_num + '.json',
            data: {
                access_token: app.globalData.gitee.access_token,
                ref: app.globalData.gitee.branch
            },
            success: function (res) {
                console.log('请求标签页列表数据成功');
                let output = app.base64.decode(res.data.content);
                let parse_res = JSON.parse(output);
                // 当前页偏移+1                
                that.data.currentTagPageIndex[dir_name] = that.data.currentTagPageIndex[dir_name] + 1;
                if (that.data.currentTagPageIndex[dir_name] >= that.data.tagsPageNum[dir_name]) {
                    that.data.isLastPage[2] = true;
                }
                
                // 获取详细统计信息
                //console.log(parse_res);
                let keys = [];
                for (let i = 0; i < parse_res.length; i++) {
                    let key = '/' + parse_res[i].key.substr(0, 4) + '/' + parse_res[i].key + '.html';
                    keys.push(key);
                }

                that.fetchArticleStatisticsData(keys, function (status, result) {
                    if (status) {
                        for (let i = 0; i < parse_res.length; i++) {
                            let j = 0;
                            for (j; j < result.length; j++) {
                                let key = '/' + parse_res[i].key.substr(0, 4) + '/' + parse_res[i].key + '.html';
                                if (key == result[j].attributes.url) {
                                    parse_res[i]["page_views"] = result[j].attributes.time == undefined ? 0 : result[j].attributes.time;
                                    parse_res[i]["comment_count"] = result[j].attributes.comments == undefined ? 0 : result[j].attributes.comments;
                                    parse_res[i]["thumbs_up_count"] = result[j].attributes.likeUserList == undefined ? 0 : result[j].attributes.likeUserList.length;
                                    break;
                                }
                            }
                            if (j == result.length) {
                                // 还未创建文章统计数据
                                parse_res[i]["page_views"] = 0;
                                parse_res[i]["comment_count"] = 0;
                                parse_res[i]["thumbs_up_count"] = 0;
                            }
                        }                
                        
                        // 回调返回结果
                        call_back(true, parse_res)
                    } else {
                        // 获取每页文章统计数据失败
                        call_back(false)
                    }
                });
            },
            fail: function (e) {
                console.log('请求博文页列表数据失败');
                call_back(false, null);
            }
        })
    },
    // 跳转详细    
    chooseArticle: function (e) {
        let key = e.currentTarget.dataset.key;
        let author = e.currentTarget.dataset.author;
        let comment_count = e.currentTarget.dataset.comment_count;
        let page_views = e.currentTarget.dataset.page_views;
        let thumbs_up_count = e.currentTarget.dataset.thumbs_up_count;
        let title = e.currentTarget.dataset.title;
        let date = e.currentTarget.dataset.date;
        let cover = e.currentTarget.dataset.cover;
        let desc = e.currentTarget.dataset.desc;
        let password = e.currentTarget.dataset.password;
        let password_desc = e.currentTarget.dataset.password_desc;
        console.log(e)
        wx.navigateTo({
            url: '../detail/detail?title=' + title + '&key=' + key + '&author=' + author
                + '&comment_count=' + comment_count + '&page_views=' + page_views
                + '&thumbs_up_count=' + thumbs_up_count + '&date=' + date + '&password=' + password
                + '&password_desc=' + password_desc + '&cover=' + cover + '&desc=' + desc + '&is_gank=true',
            success: function () {
                // nothing to do
            },
            fail: function () {
                console.error("打开链接失败");
            }
        })
    },
    // 消息订阅
    requestSubscribeMessage: function(e) {
        console.log(e)
        wx.requestSubscribeMessage({
            tmplIds: ['eWJacnVoyNj7nd-GeTZNXb0GixaP0eXNkDP3FMH7Uiw'],
            success(res) { 
                wx.showToast({
                    title: '订阅干货成功',
                    duration: 2000
                })
                // 写入openid
                const user = AV.User.current();                
                app.updateUserSubscribe(user.id, 
                    user.attributes.authData.lc_weapp.openid,
                    user.attributes.authData.lc_weapp.session_key,
                    true, 'gank')
            },fail(e) {
                console.log(e)
                wx.showToast({
                    title: '订阅失败',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    }
})